<template>
    <view>
        <scroll-view :scroll-y="true" :scroll-x="false" class="scroll" @scrolltolower="bindlower">
            <view class="wuge" v-if="allTeachers == '' || allTeachers.length == 0">没有任何教师信息</view>
            <view class="list" v-for="(item, index) in allTeachers" :key="index">
                <view class="item" :data-id="item.id + ',' + item.teacherName + ',' + item.courseCount">
                    <view class="image" :data-id="item.id + ',' + item.teacherName + ',' + item.courseCount">
                        <image :src="item.teacherHeadimgurl" />
                    </view>
                    <view class="content">
                        <text class="h1" @tap="toDetail" :data-id="item.id">
                          <td style="width:25%">{{ item.teacherName }}</td>
                          <td style="width:70%">{{ item.teacherTag }}</td>
                        </text>
                        <text class="p myp" @tap="toDetail" :data-id="item.id">
                          <span style="font-size:14px;color:#ff533b;">{{ (item.amount<=0?'免费':'￥'+item.amount)+'  ' }}</span>
                          {{ item.teacherIntroduction }}
                        </text>
                    </view>
                    <view class="number">
                        <view class="sum" @tap="toDetail" :data-id="item.id">{{ item.courseCount }}张卡片</view>
                        <button class="weui-btn" style="top:40%" @tap="buyNow" :data-id="item.id+','+item.amount+','+item.courseCount">下单</button>
                    </view>
                </view>
            </view>
        </scroll-view>
    </view>
</template>

<script>
var config = require('../../config.js');
var http = require('../../utils/httpHelper.js');
export default {
    data() {
        return {
            teacher: {},
            allTeachers: [],
            sumPrice: 0,
            timer: {},
            teacherPage: {
                current: 1,
                pageSize: 10,
                total: 0
            },
            userId: 0,
            isLogin: false,
        };
    },
    onLoad: function () {
        var that = this;
        let userInfo = uni.getStorageSync('user');
        if (userInfo != null && userInfo != undefined && userInfo != {} && userInfo != '') {
          that.setData({
              isLogin: true,
              userId: userInfo.userId
          });
        }
        that.getAllTeachers(function (list) {
            that.setData({
                allTeachers: list
            });
        });
    },
    methods: {
        getMoreData() {
            var that = this;
            that.teacherPage.current++;
            that.getAllTeachers(function (list) {
                let yList = that.allTeachers;
                yList = yList.concat(list);
                that.setData({
                    allTeachers: yList
                });
            });
        },

        getAllTeachers: function (callback) {
            var that = this;
            http.httpPost(
                config.clubApi.getTeacherjkList,
                {
                    appid: config.APPID,
                    pageNo: that.teacherPage.current,
                    pageSize: that.teacherPage.pageSize,
                    teacherType: '3'
                },
                function (res) {
                    if (res.code == '200') {
                        var list = res.data.pageData;
                        if (typeof callback == 'function') {
                            callback(list);
                        }
                    }
                }
            );
        },        

        //处理分页
        bindlower: function (e) {
          if(this.timer != null){
            clearTimeout(this.timer)
          }
          this.timer = setTimeout(()=>{
            this.getMoreData();
          },500)
        },

        buyNow: function (e) {
            if (this.isLogin && uni.getStorageSync('user')) {
                this.settlement(e);
            } else {
                uni.switchTab({
                    url: '/pages/user/index'
                });
            }
        },
        settlement: function (e) {          
            var idParam = e.currentTarget.dataset.id;
            var idAry = idParam.split(',');
            if (idAry[2]<=0) {
              uni.showToast({
                  title: '没有可下单卡片',
                  icon: 'none',
                  duration: 3000
              });
              return;
            }
            var data = {
                appId: config.APPID,
                teacherId: idAry[0],                
                userId: this.userId,
                amount: idAry[1]
            };
            http.httpPut(config.clubApi.createOrderjkt, data, function (res) {
                if (res.code == '200') {
                  if (data.amount>0) {
                    try {
                        let params = res.data;
                        WeixinJSBridge.invoke('getBrandWCPayRequest', params, function (res) {
                            if (res.err_msg == 'get_brand_wcpay_request:ok') {
                                uni.showToast({
                                    title: '支付成功',
                                    icon: 'none',
                                    duration: 3000
                                });                                
                            } else {
                                uni.showToast({
                                    title: '支付失败',
                                    icon: 'none',
                                    duration: 3000
                                });
                            }                            
                        });
                    } catch (f) {                        
                        if (res.msg) {
                            uni.showToast({
                                title: res.msg,
                                icon: 'none',
                                duration: 2000
                            });
                        }                        
                    }
                  }else {
                    if (res.msg) {
                        uni.showToast({
                            title: res.msg,
                            icon: 'none',
                            duration: 2000
                        });
                    }
                  }
                }
            }); 
        },
        toDetail: function (e) {
            var id = e.currentTarget.dataset.id;
            let userInfo = uni.getStorageSync('user');
            if (userInfo) {
              uni.navigateTo({
                  url: '../course/index?id=' + id +'&inviteId=' + userInfo.userId
              });
            }else {
              uni.navigateTo({
                  url: '../detailb/index?id=' + id
              });
            }
        },
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
